import Watcher from './observer/watcher'
import { patch } from './vdom/patch'

export function mountComponent(vm, el) {
  vm.$el = el

  callHook(vm, 'beforeMount')

  let updateComponent = () => {
    console.log('刷新页面')
    vm._update(vm._render())
  }
  new Watcher(
    vm,
    updateComponent,
    () => {
      callHook(vm, 'beforeUpdate')
    },
    true
  )

  callHook(vm, 'mounted')
}

export function lifecycleMixin(Vue) {
  Vue.prototype._update = function (vnode) {
    const vm = this
    const prevVnode = vm._vnode
    vm._vnode = vnode
    if (!prevVnode) {
      vm.$el = patch(vm.$el, vnode)
    } else {
      vm.$el = patch(prevVnode, vnode)
    }
  }
}

export function callHook(vm, hook) {
  const handlers = vm.$options[hook]
  if (handlers) {
    for (let handler of handlers) {
      handler.call(vm)
    }
  }
}
